package com.cuckoo.mall.thus.menuaccount.recently;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseListFragment;
import com.bgn.baseframe.utils.DateUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.Product;

import java.util.List;

import static com.bgn.baseframe.base.BaseActivity.ID;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class RecentlyFragment extends BaseListFragment<RecentlyContract.View, RecentlyPresenter, Product> implements RecentlyContract.View {


    private TextView tvGroup;

    @Override
    protected BaseQuickAdapter setAdapter() {
        adapter = new RecentlyViewAdapter(datas);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build("/mall/goodMultipleDetailActivity").withString(ID, datas.get(position).getProduct_id()).navigation();
            }
        });
        return adapter;
    }

    @Override
    protected void setLayoutManage() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recycleView.setLayoutManager(linearLayoutManager);
    }

    @Override
    protected void onRefreshData() {
        mPresenter.getRecentlyList(HTTP_REFRESH_LIST, 0, page_count);
    }

    @Override
    protected void initListData() {
        showLoadingView();
        mPresenter.getRecentlyList(HTTP_INIT_LIST, page_index, page_count);
    }

    @Override
    protected void loadMoreData() {
        mPresenter.getRecentlyList(HTTP_LOAD_MORE_LIST, page_index, page_count);
    }

    @Override
    public void whenGetData(List<Product> backDatas, int type) {

        switch (type) {
            case HTTP_REFRESH_LIST:
            case HTTP_INIT_LIST:
                if (backDatas == null || backDatas.size() == 0) {
                    showNoDataDefaultView(R.mipmap.default_bg_nodata, R.string.default_no_data);
                    //tvGroup.setVisibility(View.GONE);
                } else {
                    tvGroup.setText(DateUtil.getFormatString(backDatas.get(0).getLast_viewtime(), "yyyy-MM-dd"));
                    handleData(backDatas, type);
                }
                break;
            case HTTP_LOAD_MORE_LIST:
                handleData(backDatas, type);
                break;
        }


    }

    @Override
    protected void initOtherUi(View rootView) {
        super.initOtherUi(rootView);
        tvGroup = rootView.findViewById(R.id.tv_group);
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                /*获取屏幕顶部的view*/
                //View stickview = recyclerView.findChildViewUnder(0, 0);

                View transInfoView = recyclerView.findChildViewUnder(recyclerView.getMeasuredWidth(), 10);
                if (transInfoView != null) {
                    TextView titleView = transInfoView.findViewById(R.id.tv_title);
                    if (titleView != null) {
                        tvGroup.setText(titleView.getText().toString());
                    }
                }

            }
        });
    }

    @Override
    protected int getLaoutResouse() {
        return R.layout.fragment_recently_list;
    }
}
